//
//  ImageUtil.m
//  StudentClient
//
//  Created by 张宁宁 on 16/5/26.
//  Copyright © 2016年 lirenkj. All rights reserved.
//

#import "ImageUtil.h"
#import "GTMBase64.h"
#import "FileUtil.h"

@implementation ImageUtil

//转base64;
+(NSString *) image2String:(UIImage *)image {
    NSData *pictureData = UIImageJPEGRepresentation(image, 0.5);
    NSString *pictureDataString = [GTMBase64 stringByEncodingData:pictureData];
    return pictureDataString;
}

//转图片
+ (UIImage *) string2Image:(NSString *)string {
    UIImage *image = [UIImage imageWithData:[GTMBase64 decodeString:string]];
    return image;
}

+(UIImage*)getImageByName:(NSString*)name{
    NSString *path;
    if (SCREEN_SCALE>=3) {
        path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@@3x",name] ofType:@"png"];
        if (![FileUtil isExsit:path isDirectory:NO]) {
            path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@@2x",name] ofType:@"png"];
        }
        if (![FileUtil isExsit:path isDirectory:NO]) {
            path = [[NSBundle mainBundle] pathForResource:name ofType:@"png"];
        }
    }if (SCREEN_SCALE==2) {
        path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@@2x",name] ofType:@"png"];
        if (![FileUtil isExsit:path isDirectory:NO]) {
            path = [[NSBundle mainBundle] pathForResource:name ofType:@"png"];
        }
        if (![FileUtil isExsit:path isDirectory:NO]) {
            path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@@3x",name] ofType:@"png"];
        }
    }else{
        path = [[NSBundle mainBundle] pathForResource:name ofType:@"png"];
        if (![FileUtil isExsit:path isDirectory:NO]) {
            path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@@2x",name] ofType:@"png"];
        }
        if (![FileUtil isExsit:path isDirectory:NO]) {
            path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@@3x",name] ofType:@"png"];
        }
    }
  
    return  [UIImage imageWithContentsOfFile:path];
}

+ (UIImage *)resizedImageWithName:(NSString *)name
{
    return [self resizedImageWithName:name left:0.5 top:0.5];
}

+ (UIImage *)resizedImageWithName:(NSString *)name left:(CGFloat)left top:(CGFloat)top
{
    UIImage *image = [self getImageByName:name];
    return [image stretchableImageWithLeftCapWidth:image.size.width * left topCapHeight:image.size.height * top];
}
@end
